<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\View\RenderInterface;
class IndexController extends AbstractController
{
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();
        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function addData(RenderInterface $request)
    {
        $data = [
            [
                'name'=>'Peter',
                'sex' => 1,
            ],
            [
                'name'=>'Tom',
                'sex' => 1,
            ],
            [
                'name'=>'Susan',
                'sex' => 2,
            ],
            [
                'name'=>'Mary',
                'sex' => 2,
            ],
            [
                'name'=>'Jim',
                'sex' => 1,
            ],
        ];
        foreach ($data as $item) {
            $insertId[] = Db::table('db_test')->insertGetId($item);
        }
        return $insertId;
    }

    public function list()
    {
        return Db::table('db_test')->orderBy('id','desc')->get()->toArray();
    }

    public function redisSet()
    {
        $redis = ApplicationContext::getContainer()->get(Redis::class);
        return $redis->set("time", date("Y-m-d H:i:s"));
    }

    public function redisGet(){
        $redis = ApplicationContext::getContainer()->get(Redis::class);
        return $redis->get("time");
    }
}
